package com.fredy.mvp;

import android.content.Context;

import java.lang.ref.WeakReference;

public abstract class BasePresenter<M extends Model, V extends BaseView> implements Presenter<M, V> {
 
    /**
     * 弱引用防止内存泄漏
     */
    private WeakReference<V> wrf;
    public M model;
    public Context mContext;


    public BasePresenter(Context context) {
        mContext = context;
    }

    @Override
    public void registerModule(M model) {
        this.model = model;
    }

    @Override
    public void registerView(V view) {
        wrf = new WeakReference(view);
    }

    @Override
    public V getView() {
        return wrf == null ? null : wrf.get();
    }

    @Override
    public void destroy() {
        if (wrf != null) {
            wrf.clear();
            wrf = null;
        }
        onViewDestroy();
    }

    protected abstract void onViewDestroy();


}
